package com.dodo.blog.ui.util;

import com.dodo.blog.WebApplication;
import com.dodo.blog.model.Article;
import com.dodo.blog.model.Category;
import com.dodo.blog.model.Tag;
import com.dodo.blog.ui.page.Page;
import com.dodo.blog.ui.page.publish.Articles;
import org.apache.commons.lang.StringUtils;

/**
 * @author <a href="mailto:pohorelec@comvai.com">Jozef Pohorelec</a>
 */
public class UriConstructor
{
    public static final String PARAM_ARTICLE_ID = "id";

    public static final String PARAM_ARTICLE_DATE = "date";

    public static final String PARAM_ARTICLE_CATEGORY = "category";

    public static final String PARAM_ARTICLE_TAG = "tag";

    private static final String SLASH = "/";

    public static String createArticleUri( Article article )
    {
        if ( article == null || StringUtils.isBlank( article.getNormalizedName() ) )
        {
            return "";
        }
        return constructUri( Articles.class, PARAM_ARTICLE_ID, article.getId().toString(), article.getNormalizedName() );
    }

    public static String createCategoyUri( Category category )
    {
        if ( category == null || StringUtils.isBlank( category.getNormalizedName() ) )
        {
            return "";
        }
        return constructUri( Articles.class, PARAM_ARTICLE_CATEGORY, category.getNormalizedName() );
    }

    public static String createTagUri( Tag tag )
    {
        if ( tag == null || StringUtils.isBlank( tag.getNormalizedName() ) )
        {
            return "";
        }
        return constructUri( Articles.class, PARAM_ARTICLE_TAG, tag.getNormalizedName() );
    }

    public static String createDateUri( String year, String month, String day )
    {
        if ( year == null && month == null && day == null )
        {
            return "";
        }
        return constructUri( Articles.class, PARAM_ARTICLE_DATE, year, month, day );
    }

    private static String constructUri( Class<? extends Page> page, String... params )
    {
        String path = WebApplication.get().getPathByPageClass( page );
        path = path.replace( "/*", "" );
        StringBuilder sb = new StringBuilder();
        sb.append( path );

        for ( String p : params )
        {
            if ( p == null )
            {
                continue;
            }
            sb.append( SLASH );
            sb.append( p );
        }

        return sb.toString();
    }

}
